/**
 * @desc a toast plugin for mobile
 */
import ToastComponent from './dialog.vue'
import Vue from 'vue';
/**
 * @param {String} message 提示文案
 * @param {String} btnPreText 按钮文案 默认为取消
 * @param {String} btnNextText 按钮文案 默认为确定
 * @return Promise 返回Promise 点击右侧按钮 可以链式调用 Message("测试").then(fun)
 */
let tempContainer = null
let ToastController = Vue.extend(ToastComponent);
let instance = new ToastController().$mount(document.createElement("div"));
if (document.querySelector('.messes-dialog')) {
    document.body.removeChild(document.querySelector('.messes-dialog'))
}
document.body.appendChild(instance.$el);
function messesDialog(o) {
    // let instance = new ToastController().$mount(document.createElement("div"));
    // if (document.querySelector('.messes-dialog')) {
    //     document.body.removeChild(document.querySelector('.messes-dialog'))
    // }
    // document.body.appendChild(instance.$el);
    
    o = o || {};
    let defaultO = {
        btnPreText: '取消',
        btnNextText: '确认',
        title: '提示',
        message: '是否删除 ?',
        hideBtnPre: false,
        hideBtnNext: false
    };
    for(var i in defaultO){
        if(!o[i]){
            o[i] = defaultO[i]
        }
    }
    tempContainer = o
    instance.message = o.message;
    instance.title = o.title;
    instance.btnPreText = o.btnPreText;
    instance.btnNextText = o.btnNextText;
    instance.hideBtnPre = o.hideBtnPre;
    instance.hideBtnNext = o.hideBtnNext;
    instance.isShowDialog = true;
    return new Promise(function (resolve, reject) {
        instance.resolve = resolve;
        instance.reject = reject;
    }).catch(e => {
        console.log(e)
    });
}
messesDialog.update = o => {
    // let div = document.querySelector('.messes-dialog')
    // console.log(div)
    // let instance = new ToastController().$mount(div);
    for(var i in tempContainer){
        if(!o[i]){
            o[i] = tempContainer[i]
        }
    }
    instance.message = o.message;
    instance.title = o.title;
    instance.btnPreText = o.btnPreText;
    instance.btnNextText = o.btnNextText;
    instance.hideBtnPre = o.hideBtnPre;
    instance.hideBtnNext = o.hideBtnNext;
    // instance.visible = true;
    return new Promise(function (resolve, reject) {
        instance.resolve = resolve;
        instance.reject = reject;
    }).catch(e => {
        console.log(e)
    });
}
messesDialog.close = () => {
    instance.isShowDialog = false;
}
export default messesDialog
